/*
 * Copyright (c) 2018. Beyondsoft Corporation.
 * All Rights Reserved.
 *
 * BEYONDSOFT CONFIDENTIALITY
 *
 * The information in this file is confidential and privileged from Beyondsoft Corporation,
 * which is intended only for use solely by Beyondsoft Corporation.
 * Disclosure, copying, distribution, or use of the contents of this file by persons other than Beyondsoft Corporation
 * is strictly prohibited and may violate applicable laws.
 */

package com.beyondsoft.smarthome.base.view;

import android.Manifest;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import com.beyondsoft.smarthome.R;
import com.beyondsoft.smarthome.base.presenter.BasePresenter;
import com.beyondsoft.smarthome.utils.FileUtils;
import com.beyondsoft.smarthome.utils.MvpInstantiationUtils;
import com.beyondsoft.smarthome.utils.ToastUtil;
import com.beyondsoft.smarthome.utils.logs.LogTag;

/**
 * @author xzk
 * @data 2019/2/26
 * @email xiezhengkun@beyondsoft.com
 * @remark 基类activity，
 */
public abstract class BaseActivity<T extends BasePresenter> extends BaseNoModelActivity  {
    protected T presenter;
    //状态栏高度px
    private int statusBarHeight;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        presenter = MvpInstantiationUtils.getInstantiation(
            this,
            MvpInstantiationUtils.FIRSTGENERICITY);
        if (presenter != null) {
            presenter.bindViewAndModel(this);
        }
        super.onCreate(savedInstanceState);
        getStateBar3();
    }

    private void getStateBar3(){
        int result = 0;
        int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = this.getResources().getDimensionPixelSize(resourceId);
        }
        LogTag.d("状态栏高度3：" + result + "px");
        statusBarHeight = result;
        View byId = findViewById(R.id.v_title_on);
        try{
            if(byId!=null){
                ViewGroup.LayoutParams params = byId.getLayoutParams();
                params.height=result;
                LogTag.d("setAct:"+this.getLocalClassName()+"-->"+params.height);
                byId.setLayoutParams(params);
            }else {
                LogTag.d("actName:"+this.getLocalClassName());
            }
        }catch (Exception e){
            e.printStackTrace();
            LogTag.d("actName:"+this.getLocalClassName());
        }
    }

    public int getStatusBarHeight() {
        return statusBarHeight;
    }

    /**
     * 拨打电话（直接拨打电话）
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum){
        requestPermission(200, new String[]{Manifest.permission.CALL_PHONE,
                                                    Manifest.permission.READ_PHONE_STATE}, () -> {
            Intent intent = new Intent(Intent.ACTION_CALL);
            Uri data = Uri.parse("tel:" + phoneNum);
            intent.setData(data);
            startActivity(intent);
        }, () -> {
            //权限失败
            // Oups permission denied
            ToastUtil.showShort(R.string.str_request_callphone);
        });
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone2EditPhone(String phoneNum) {
        requestPermission(100, new String[]{Manifest.permission.CALL_PHONE,
                                            Manifest.permission.READ_PHONE_STATE}, () -> {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + phoneNum);
            intent.setData(data);
            startActivity(intent);
        }, () -> {
            //权限失败
            // Oups permission denied
            ToastUtil.showShort(R.string.str_request_callphone);
        });
    }

    /**
     * 赋值内容到剪切板
     * @param cpContent
     */
    public void copyContentIn(String cpContent){
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", cpContent);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        ToastUtil.showLong(cpContent + getString(R.string.str_copy_success));
    }
//为关闭刷新保留的方法
    public void closeRefresh(){ }
    @Override
    protected void onDestroy() {
        if(presenter != null){
            presenter.recycleResource();
            presenter = null;
        }
        super.onDestroy();
    }
}
